package com.granada.controller.sys;


import com.granada.behaviour.neutral.PermissionWithNeutralBehavior;
import com.granada.behaviour.neutral.RolePermissionWithNeutralBehavior;
import com.granada.behaviour.neutral.RoleWithNeutralBehavior;
import com.granada.entity.Ao;
import com.granada.entity.Page;
import com.granada.entity.SysRole;
import com.granada.service.RoleWithBigService;
import com.granada.service.base.RoleService;
import com.granada.utils.DataResult;
import com.granada.aop.annotation.LogAnnotation;
import com.granada.model.pc.RoleWithBig;
import com.granada.vo.req.RoleAddReqVO;
import com.granada.vo.req.RolePageReqVO;
import com.granada.vo.req.RoleUpdateReqVO;
import com.granada.vo.resp.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 开发角色管理
 */
@RestController
@Api(tags = "系统管理-企业管理")
@RequestMapping("/sys")
public class SysRoleController {
    @PostMapping("/ping/role")
    @ApiOperation(value = "新增角色接口")
    @LogAnnotation(title = "角色管理",action = "新增角色")
    @RequiresPermissions("sys:ping:role:insert")
    public DataResult<SysRole> addRole(@RequestBody @Valid RoleAddReqVO vo) throws Exception {
        DataResult<SysRole> result= DataResult.success();
        RoleService roleService =  new RoleWithBigService(
                new RoleWithBig(
                        vo,
                        null,
                        null,
                        Ao.builder().permissionIds(vo.getPermissionIds()).build()
                ),
                new RoleWithNeutralBehavior(),
                new PermissionWithNeutralBehavior(),
                new RolePermissionWithNeutralBehavior(),
                Ao.builder().permissionIds(vo.getPermissionIds()).build()

        );
        roleService.add();
//        result.setData(roleService.bigRole(vo));
        return result;
    }
    @DeleteMapping("/ping/role/{id}")
    @ApiOperation(value = "删除角色接口")
    @LogAnnotation(title = "角色管理",action = "删除角色")
    @RequiresPermissions("sys:ping:role:delete")
    public DataResult deleted(@PathVariable("id") Long id) throws Exception {
        RoleService roleService =  new RoleWithBigService(
                new RoleWithBig(
                        null,
                        null,
                        null,
                        Ao.builder().id(id).build()
                ),
                new RoleWithNeutralBehavior(),
                new PermissionWithNeutralBehavior(),
                new RolePermissionWithNeutralBehavior(),
                Ao.builder().id(id).build()
        );
        roleService.delete();
//        roleService.deletedRole(id);
        return DataResult.success();
    }
    @PutMapping("/ping/role")
    @ApiOperation(value = "更新角色信息接口")
    @LogAnnotation(title = "角色管理",action = "更新角色信息")
    @RequiresPermissions("sys:ping:role:update")
    public DataResult updateDept(@RequestBody @Valid RoleUpdateReqVO vo, HttpServletRequest request) throws Exception {
        RoleService roleService =  new RoleWithBigService(
                new RoleWithBig(
                        vo,
                        null,
                        null,
                        Ao.builder().id(vo.getId()).permissionIds(vo.getPermissionIds()).build()
                ),
                new RoleWithNeutralBehavior(),
                new PermissionWithNeutralBehavior(),
                new RolePermissionWithNeutralBehavior(),
                Ao.builder().id(vo.getId()).permissionIds(vo.getPermissionIds()).build()
        );
        roleService.update();
//        roleService.updateRole(vo,request.getHeader(Constant.ACCESS_TOKEN));
        return DataResult.success();
    }
    @GetMapping("/ping/role/{id}")
    @ApiOperation(value = "查询角色详情接口")
    @LogAnnotation(title = "角色管理",action = "查询角色详情")
    @RequiresPermissions("sys:ping:role:one")
    public DataResult<SysRole> detailInfo(@PathVariable("id") Long id) throws Exception {
        DataResult<SysRole> result=DataResult.success();
        RoleService roleService =  new RoleWithBigService(
                new RoleWithBig(
                        null,
                        null,
                        null,
                        Ao.builder().id(id).build()
                ),
                new RoleWithNeutralBehavior(),
                new PermissionWithNeutralBehavior(),
                new RolePermissionWithNeutralBehavior(),
                Ao.builder().id(id).build()
        );
//        roleService.getOne();
        result.setData((SysRole) roleService.getOne());
        return result;
    }
    @GetMapping("/ping/role")
    @ApiOperation(value = "分页获取角色信息接口")
    @LogAnnotation(title = "角色管理",action = "分页获取角色信息")
    @RequiresPermissions("sys:ping:role:list")
    public DataResult<PageVO<SysRole>> pageInfo(RolePageReqVO vo) throws Exception {
        DataResult<PageVO<SysRole>> result=DataResult.success();
        RoleService roleService =  new RoleWithBigService(
                new RoleWithBig(
                        vo,
                        null,
                        Page.builder().pageNum(vo.getPageNum()).pageSize(vo.getPageSize()).build(),
                        null
                ),
                new RoleWithNeutralBehavior(),
                new PermissionWithNeutralBehavior(),
                new RolePermissionWithNeutralBehavior(),
                null
        );

        result.setData(roleService.getList());
//        result.setData(roleService.bigPageInfo(vo));
        return result;
    }

}
